﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Drawing;
using SiteProjects.Classes;

/*
Developed By: Sujeet kumar.
Developed On: 11 Aug 2011. 
projDesc: The Code is developed for a Request of Project by the user and admin get a mail with the detail of user ID , User Name 
 * and Project Name.
*/

namespace SiteProjects.Request_Project
{
    public partial class Request_ProjectUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Clear the controls if the cancel button is clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imbCancel_Click(object sender, ImageClickEventArgs e)
        {
            txtProjectName.Text = "";
            txtDescription.Text = "";
            lblMessage.Text = "";
        }

        /// <summary>
        /// Following will send the request to Administrator for new Project.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imbSendProjectRequest_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string currentUrl;
                string subject = string.Empty;
                string userID = string.Empty;
                string userName = string.Empty;
                string[] arrMail = null;
                string emailBody = string.Empty;

                SPSite webSite = SPContext.Current.Site;
                string mailTo = System.Configuration.ConfigurationManager.AppSettings["RicAdmin"].ToString(); // The Mail is sending to the RicAdmin //
                arrMail = new string[] { mailTo };

                SPWeb web = SPContext.Current.Web; //web object get all the current data on sharepoint application
                userID = web.CurrentUser.LoginName.ToString(); // get the login name

                //Condition check to fetch the junk value in the sharepoint variable.
                if (userID.ToString().Contains("f|sqlmember|") || userID.ToString().Contains("i:0#.w|"))
                {
                    string userIDIndex = userID.ToString().Substring(userID.ToString().LastIndexOf('|'));
                    userID = userIDIndex.Trim('|').Trim().ToString();
                }
                else
                {
                    userID = web.CurrentUser.LoginName.ToString();
                }

                //check this url exists or not
                string mailFormatPath = Server.MapPath("~/_layouts/RequestProject/RequestProject.htm");
                if (File.Exists(mailFormatPath))
                {
                    currentUrl = Request.Url.ToString(); // get the current url
                    userName = web.CurrentUser.Name.ToString(); // get the name

                    // its get the value of the user and sending to the mail formate page userFormate.htm //
                    StreamReader sr = new StreamReader(mailFormatPath);
                    emailBody = sr.ReadToEnd();
                    sr.Dispose();
                    emailBody = emailBody.Replace("@@@@projectName", txtProjectName.Text.ToString().Trim());
                    emailBody = emailBody.Replace("@@@@description", txtDescription.Text.ToString().Trim());
                    emailBody = emailBody.Replace("@@@@url", currentUrl.ToString().Trim());
                    emailBody = emailBody.Replace("@@@@userId", userID.ToString().Trim());
                    emailBody = emailBody.Replace("@@@@firstName", userName.ToString().Trim());

                }
                subject = "Creation of New RIC Project: " + txtProjectName.Text.ToString().Trim();

                //call email function to send the mail 
                Email objEmail = new Email();
                if (objEmail.SendMail(arrMail, string.Empty, string.Empty, subject, emailBody))
                {
                    txtDescription.Text = "";
                    txtProjectName.Text = "";
                    StringBuilder msgScript = new StringBuilder();
                    msgScript.Append("<script language='javascript'>");
                    msgScript.Append("alert('Your request for project has been registered. You will receive email confirmation once the project has been created. Click OK to return to your Home page.');");
                    msgScript.Append("window.location=window.location;");
                    msgScript.Append("</script>");
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", msgScript.ToString());
                }
                else
                {
                    lblMessage.Text = "Your request could not be submitted successfully, please try again later!";
                    lblMessage.ForeColor = Color.Red;
                }
            }
            catch
            {
                lblMessage.Text = "Your request could not be submitted successfully, please try again later!";
                lblMessage.ForeColor = Color.Red;
            }
        }
    }
}
